@SET MY_DIR=%~dp0

@IF "%1"=="" @exit /b 1

@CALL :%1 "%~2" "%~3" "%~4" "%~5" "%~6" || @exit /b 1

@exit /b 0

REM **********************************************************
:defvmuxcmd

SET CLIPN=%~1

SET EXP=%avs_video_path%.clip%CLIPN%.264
CALL :expvar || exit /b 1
SET VIDEO_PATH=%EXP%

call :chexist "%VIDEO_PATH%" || exit /b 1

SET muxer_video_cmd=-add "%VIDEO_PATH%:fps=%avs_framerate%"

exit /b 0

REM **********************************************************
:defamuxcmd

SET /A DELAY=%~1
SET /A CLIPN=%~2

IF "%avs_audio_num%"=="0" exit /b 0
SET /A AUDIO_NUM=%avs_audio_num%+1
SET avs_audio_no=1

SET muxer_audio_cmd=

:defmuxcmd

IF "%avs_audio_no%"=="%AUDIO_NUM%" exit /b 0

SET EXP=%avs_audio_path%.clip%CLIPN%.track%avs_audio_no%.%DELAY%ms
CALL :expvar || exit /b 1
SET AUDIO_PATH=%EXP%.m4a

SET /A track_id=%avs_audio_no%-1
SET muxer_audio_cmd=%muxer_audio_cmd% -add "%AUDIO_PATH%#trackID=%track_id%"

SET /A avs_audio_no=%avs_audio_no%+1
GOTO defmuxcmd
exit /b 0

REM **********************************************************
:rawmux
COLOR 5F

SET SKIP=%~1
IF NOT "%SKIP%"=="n" SET SKIP=y

SET POSTF=%~2

SET EXP=%avs_mux_path%%POSTF%
CALL :expvar || exit /b 1
SET MUX_PATH=%EXP%.mp4

call :chexist "%MUX_PATH%" && IF "%SKIP%"=="y" ECHO File %MUX_PATH% exists, skipping... & exit /b 0

call :mdforfile "%MUX_PATH%" || exit /b 1

echo ##### Mux encoding cmd: %muxer_video_cmd% %muxer_audio_cmd% -tmp "%TEMP_DIR%" -new "%MUX_PATH%"

IF "!avs_ripper!"=="" SET avs_ripper=UnknownRipper

%LOW_PRIORITY% "%MP4BOX_PATH%" %muxer_video_cmd% %muxer_audio_cmd% -tmp "%TEMP_DIR%" -itags comment="Encoded by %avs_ripper% using AVSB %AVSB_VER%" -new "%MUX_PATH%"

call :chexist "%MUX_PATH%" || exit /b 1

exit /b 0

REM **********************************************************
:rawvenc
COLOR 6F

echo ##### Video passes: %avs_passes_num%

SET SKIP=%~1
IF NOT "%SKIP%"=="n" SET SKIP=y

SET CLIPN=%~4

IF "%avs_passes_num%"=="1" SET EXP=%avs_video_path%.clip%CLIPN%.264
IF NOT "%avs_passes_num%"=="1" SET EXP=%avs_stats1_path%.clip%CLIPN%.stats
CALL :expvar || exit /b 1
SET VIDEO_OUT_PATH=%EXP%

call :chexist "%VIDEO_OUT_PATH%" && IF "%SKIP%"=="y" ECHO File %VIDEO_OUT_PATH% exists, skipping... & GOTO encodev2

call :mdforfile "%VIDEO_OUT_PATH%" || exit /b 1

SET EXP=%avs_pass_1_cmd%
CALL :expvar || exit /b 1
SET VIDEO_CMD=%EXP%

echo ##### Video pass 1 encoding cmd: %VIDEO_CMD%

%LOW_PRIORITY% "%OPENAVS_PATH%" -v "avs_clip_no=%CLIPN%" "avs_no_audio=true" -f "%SCRIPT_PATH%" -y4m | %LOW_PRIORITY% "%X264_PATH%" %VIDEO_CMD%

:encodev2

IF "%avs_passes_num%"=="1" exit /b 0

SET VIDEO_IN_PATH=%VIDEO_OUT_PATH%

SET SKIP=%~2
IF NOT "%SKIP%"=="n" SET SKIP=y

IF "%avs_passes_num%"=="2" SET EXP=%avs_video_path%.clip%CLIPN%.264
IF NOT "%avs_passes_num%"=="2" SET EXP=%avs_stats2_path%.clip%CLIPN%.stats
CALL :expvar || exit /b 1
SET VIDEO_OUT_PATH=%EXP%

call :mdforfile "%VIDEO_OUT_PATH%" || exit /b 1

call :chexist "%VIDEO_OUT_PATH%" && IF "%SKIP%"=="y" ECHO File %VIDEO_OUT_PATH% exists, skipping... & GOTO encodev3

SET EXP=%avs_pass_2_cmd%
CALL :expvar || exit /b 1
SET VIDEO_CMD=%EXP%

echo ##### Video pass 2 encoding cmd: %VIDEO_CMD%

%LOW_PRIORITY% "%OPENAVS_PATH%" -v "avs_clip_no=%CLIPN%" "avs_no_audio=true" -f "%SCRIPT_PATH%" -y4m | %LOW_PRIORITY% "%X264_PATH%" %VIDEO_CMD%

:encodev3

IF "%avs_passes_num%"=="2" exit /b 0

SET VIDEO_IN_PATH=%VIDEO_OUT_PATH%

SET SKIP=%~3
IF NOT "%SKIP%"=="n" SET SKIP=y

SET EXP=%avs_video_path%.clip%CLIPN%.264
CALL :expvar || exit /b 1
SET VIDEO_OUT_PATH=%EXP%

call :chexist "%VIDEO_OUT_PATH%" && IF "%SKIP%"=="y" ECHO File %VIDEO_OUT_PATH% exists, skipping... & exit /b 0

call :mdforfile "%VIDEO_OUT_PATH%" || exit /b 1

SET EXP=%avs_pass_3_cmd%
CALL :expvar || exit /b 1
SET VIDEO_CMD=%EXP%

echo ##### Video pass 3 encoding cmd: %VIDEO_CMD%

%LOW_PRIORITY% "%OPENAVS_PATH%" -v "avs_clip_no=%CLIPN%" "avs_no_audio=true" -f "%SCRIPT_PATH%" -y4m | %LOW_PRIORITY% "%X264_PATH%" %VIDEO_CMD%
exit /b 0

REM **********************************************************
:rawaenc
echo ##### Audio tracks: %avs_audio_num%

COLOR 3F

SET SKIP=%~1
IF NOT "%SKIP%"=="n" SET SKIP=y

SET /A DELAY=%~2
SET /A CLIPN=%~3

IF "%avs_audio_num%"=="0" exit /b 0
SET /A AUDIO_NUM=%avs_audio_num%+1
SET avs_audio_no=0

:encodeaudio

SET /A avs_audio_no=%avs_audio_no%+1

IF "%avs_audio_no%"=="%AUDIO_NUM%" exit /b 0

SET EXP=%avs_audio_path%.clip%CLIPN%.track%avs_audio_no%.%DELAY%ms
CALL :expvar || exit /b 1
SET AUDIO_PATH=%EXP%.m4a

call :chexist "%AUDIO_PATH%" && IF "%SKIP%"=="y" ECHO File %AUDIO_PATH% exists, skipping... & GOTO encodeaudio

SET AUDIO_CMD_IN=--script "avs_audio_no=!avs_audio_no! avs_clip_no=!CLIPN! avs_ext_delay=%DELAY% import(^^!SCRIPT_PATH!^^)"

SET EXP=%avs_audio_cmd%
CALL :expvar || exit /b 1
SET AUDIO_CMD_OUT=%EXP%

call :mdforfile "%AUDIO_PATH%" || exit /b 1

echo ##### Audio %avs_audio_no% encoding cmd: %AUDIO_CMD_OUT%

%LOW_PRIORITY% "%BEPIPE_PATH%" %AUDIO_CMD_IN% | %LOW_PRIORITY% "%NEROAAC_PATH%" %AUDIO_CMD_OUT%

call :chexist "%AUDIO_PATH%" || exit /b 1

GOTO encodeaudio

exit /b 0

REM **********************************************************
:updver
@SET VERSIONS_PATH=%MY_DIR%LIB\__ver.avsx

@call :chexist "%VERSIONS_PATH%" || @exit /b 1

@FOR /F "usebackq tokens=5" %%i IN (`"%MP4BOX_PATH%" -version`) DO @IF "!avs_mp4box_ver!"=="" @SET avs_mp4box_ver=%%i
@FOR /F "usebackq tokens=2" %%i IN (`"%X264_PATH%" --version`) DO @IF "!avs_x264_ver!"=="" @SET avs_x264_ver=%%i
@FOR /F "usebackq tokens=1" %%i IN (`cscript /B "%MY_DIR%LIB\version.vbs" "file" "%NEROAAC_PATH%"`) DO @IF "!avs_neroaac_ver!"=="" @SET avs_neroaac_ver=%%i

@echo avs_tool="AVSB %AVSB_VER%" > "%VERSIONS_PATH%"
@echo avs_video_encoder="x264 %avs_x264_ver%" >> "%VERSIONS_PATH%"
@echo avs_audio_encoder="Nero AAC %avs_neroaac_ver%" >> "%VERSIONS_PATH%"
@echo avs_muxer="MP4Box %avs_mp4box_ver%" >> "%VERSIONS_PATH%"
@exit /b 0

REM **********************************************************
:chexist

@IF EXIST "%~1" @EXIT /b 0
@echo File %1 does not exist
@exit /b 1

REM **********************************************************
:mdforfile

@IF EXIST "%~dp1" @EXIT /b 0
@md "%~dp1" && exit /b 0
echo Can`t create dir "%~dp1"
@exit /b 1

REM **********************************************************
:expvar

@IF NOT ""=="!EXPEXPEXP!" @exit /b 1

@SET EXP=%EXP:"=````%
:expandv
@SET EXP=%EXP%
@IF NOT "%EXP%"=="!EXP!" @GOTO expandv
@SET EXP=%EXP:````="%

@exit /b 0

REM **********************************************************
:gettoken

@SET GT_TOKEN=
@SET GT_STRING=%~1

@IF "%GT_STRING%"=="" @exit /b 0

@FOR /F "tokens=1" %%i IN ("%GT_STRING%") DO @SET GT_TOKEN=%%i
@FOR /F "tokens=1*" %%i IN ("%GT_STRING%") DO @SET GT_STRING=%%j

@exit /b 0